package com.zhou.base.components.bs.common.exception.base;

import cn.hutool.core.util.StrUtil;
import com.zhou.base.components.bs.common.util.MessageUtils;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

/**
 * 业务异常
 *
 * @author ruoyi
 */
@AllArgsConstructor
@NoArgsConstructor
public class BaseException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    private Integer code;

    /**
     * 错误提示
     */
    private String message;

    /**
     * 错误提示
     */
    private String messageCode;

    private Object[] args;

    public BaseException(Throwable cause) {
        super(cause);
        this.message = cause.toString();
    }

    public Integer getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        String message = this.message;
        if (StrUtil.isNotBlank(messageCode)) {
            message = MessageUtils.message(messageCode, args, message);
        }
        return message;
    }


    public BaseException code(Integer code) {
        this.code = code;
        return this;
    }

    public BaseException message(String message) {
        this.message = message;
        return this;
    }

    public BaseException messageCode(String messageCode) {
        this.messageCode = messageCode;
        return this;
    }

    public BaseException args(Object... args) {
        this.args = args;
        return this;
    }
}
